package com.sinping.qh.api.app.version.v1.records;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.*;
import com.sinping.qh.dto.app.records.CheckRecordByAppDTO;
import com.sinping.qh.dto.app.records.CheckRecordOffLineByAppDTO;
import com.sinping.qh.dto.common.CommonDTO;
import com.sinping.qh.service.app.records.ICheckRecordService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangjin
 * @create 2018/5/31
 */
@Api(value = "app检查记录",description = "app检查记录API")
@RestController
@RequestMapping(value =  Const.APP_URI_V1+"/checkrecord",name="检查记录")
public class CheckRecordEndPointV1 extends BaseAppEndPoint {

    @Autowired
    ICheckRecordService checkRecordService;


    /***
     * 根据检查计划id,和检查记录状态查询即检查详情
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "检查计划下的检查记录")
    @RequestMapping(value="checkplan",name = "检查计划下的检查记录", method = RequestMethod.GET)
    public ResDto listbyCheckPlanId(@ApiParam(name="msg",
            value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
                    ", \"condition\":{\"checkplan_id\":\"123\",\"check_status\":\"[0123]\"}}")
                                @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<CheckRecordByAppDTO> frontPage = JsonMapper.INSTANCE.fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        //获取数据
        frontPage =checkRecordService.findPage(frontPage);

        checkNull(frontPage, ConstEum.FAIL);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        return resDto;
    }
    @ApiOperation(httpMethod = "GET", value = "楼宇分组列表")
    @RequestMapping(value="uniting",name = "楼宇分组列表", method = RequestMethod.GET)
    public ResDto listbyuniting(@ApiParam(name="msg",
            value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
                    ", \"condition\":{}}")
                                @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<CheckRecordByAppDTO> frontPage = JsonMapper.INSTANCE.fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());

        //获取数据
        frontPage =checkRecordService.findPageByUniting(frontPage,commonDTO);

        checkNull(frontPage,ConstEum.FAIL);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        return resDto;
    }

    @ApiOperation(httpMethod = "GET", value = "楼宇分组详情列表")
    @RequestMapping(value="unitingdetail",name = "楼宇分组详情列表", method = RequestMethod.GET)
    public ResDto listbyunitingDetail(@ApiParam(name="msg",
            value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
                    ", \"condition\":{\"check_unit_id\":\"123\",\"check_building_id\":\"testbuild\"}}")
                                @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<CheckRecordByAppDTO> frontPage = JsonMapper.INSTANCE.fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());
        //获取数据
        frontPage =checkRecordService.findPageByUnitingDetail(frontPage,commonDTO);

        checkNull(frontPage,ConstEum.FAIL);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        return resDto;
    }

    @ApiOperation(httpMethod = "GET", value = "记录页面列表")
    @RequestMapping(value="rectifyandreview",name = "记录页面列表", method = RequestMethod.GET)
    public ResDto listByRectifyAndReview(@ApiParam(name="msg",
            value="{\"page_size\":10, \"page_current\":1, \"order_column\":\"gmt_created\", \"order_by\":\"desc\"" +
                    ", \"condition\":{\"check_status\":\"[0123]\",\"unit_name\":\"\"" +
                    ",\"start_time\":\"2018-07-01 00:00:00\",\"end_time\":\"2018-07-01 00:00:00\"}}")
                                         @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        FrontPage<CheckRecordByAppDTO> frontPage = JsonMapper.INSTANCE.fromJson(msg, FrontPage.class);

        //查询时检查查询条件格式
        checkNull(frontPage,ConstEum.PARAM_ERROR);

        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());

        //获取数据
        frontPage =checkRecordService.findPageByRectifyAndReview(frontPage,commonDTO);

        checkNull(frontPage,ConstEum.FAIL);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(frontPage));
        return resDto;
    }

    @ApiOperation(httpMethod = "DELETE", value = "删除检查记录")
    @RequestMapping(value = "/{id}",name = "删除检查记录", method = RequestMethod.DELETE)
    public ResDto deleteRecord(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);
        checkRecordService.delete(id);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }
    @ApiParam(name = "id")
    @ApiOperation(httpMethod = "GET", value = "检查记录明细")
    @RequestMapping(value = "/{id}",name = "检查记录明细", method = RequestMethod.GET)
    public ResDto recoredDetail(@PathVariable String id)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CheckRecordByAppDTO checkRecordsByAppDTO=  checkRecordService.findOne(id);


        String data=JsonMapper.defaultMapper().toJson(checkRecordsByAppDTO);
        resDto.setDatas(data);

        return resDto;
    }


    @ApiOperation(httpMethod = "PUT", value = "修改检查记录状态")
    @RequestMapping(value="examine",name = "修改检查记录状态", method = RequestMethod.PUT)
    public ResDto recoredExamine(@ApiParam(name="msg",
            value="{\"id\":[\"5b14fc8c6a58d4226c20a701\"],\"check_status\":\"1\"}")
                                     @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        Condition condition = JsonMapper.INSTANCE.fromJson(msg, Condition.class);

        for (String id:condition.getId()) {

            checkRecordService.examineCheckRecord(id,condition.getCheckStatus());
        }
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }

    @ApiOperation(httpMethod = "PUT", value = "修改检查记录离线")
    @RequestMapping(value="update_offline",name = "修改检查记录离线", method = RequestMethod.PUT)
    public ResDto updateRecordByOffline(@ApiParam(name="msg",
            value="{\"id\":\"5b14fc8c6a58d4226c20a701\",\"check_status\":\"1\",\"check_users_id\":[\"textuser\"],\n" +
                    "\"union_unit_users\":\"zhng\",\n" +
                    "\"sub_items\":{\"id\":\"\",\"check_id\":\"123\",\"desc\":\"testunit\"," +
                    "\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\n" +
                    "\"create_user_id\":\"123\",\"create_user_name\":\"zhj\"},"+
                    "\"hiddendanger_item\":[{\"id\":\"\",\"check_id\":\"456\",\"check_unit\":\"123\",\"check_unit_id\":\"testunit\",\n" +
            "\"check_building\":\"estbuild\" ,\"check_building_id\":\"textname\",\n" +
            "\"check_lab\":\"textuser\",\"check_lab_id\":\"textuser\",\"check_room\":\"textid\",\n" +
            "\"check_item_one\":\"textudpateid\",\"check_item_one_id\":\"123\"," +
            "\"check_item_two\":\"textudpateid\",\"check_item_two_id\":\"123\",\"desc\":\"电线安全问题\"" +
            ",\"image_url\":[{\"id\":\"ac88fec3b85545c6b00941a915aca925\",\"img_path\":\"\"}],\"check_item_three\":\"123\",\"check_item_three_id\":\"asdf\",\n" +
            "\"hidden_danger_status\":\"0\",\"create_user_id\":\"123\",\"create_user_name\":\"zhj\"}]}")
                                 @RequestParam(required = true) String msg)
    {
        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CheckRecordOffLineByAppDTO checkRecordOffLineByAppDTO =
                JsonMapper.INSTANCE.fromJson(msg,CheckRecordOffLineByAppDTO.class);
        //查询时检查查询条件格式
        checkNull(checkRecordOffLineByAppDTO,ConstEum.PARAM_ERROR);

        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setUserSessionDTO(getUserSessionDto());

        checkRecordService.updateRecordByOffline(checkRecordOffLineByAppDTO,commonDTO);
        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        return resDto;
    }


}
